<?php
namespace Index\Controller;
use Think\Controller;
class FocusController extends Controller {
    /**
     *关注
     */
    public function indexAction(){
        $qid = I("get.id");
        $uid = I("session.uid",0);
        $model = M();
        $sql = "select * from focus where qid = $qid and uid = $uid ";
        $exist = $model -> query($sql);
        if(empty($exist)){
            $sql = "insert into focus (uid,qid) values('$uid','$qid')";
            $model->execute($sql);
            $sql = "select attention from question where id = $qid ";
            $attentionData = $model -> query($sql);
            $attention = $attentionData[0]['attention'] + 1;
            $sqlAttention = "update question set attention = '$attention' where id = $qid";
            $model->execute($sqlAttention);
            $this->success('关注成功',U("Index/Question/show/id/$qid"),2);
        }else{
            $sql = "delete from focus where qid = $qid and uid = $uid ";
            $model->execute($sql);
            $sql = "select attention from question where id = $qid ";
            $attentionData = $model -> query($sql);
            $attention = $attentionData[0]['attention'] - 1;
            $sqlAttention = "update question set attention = '$attention' where id = $qid";
            $model->execute($sqlAttention);
            $this->success('取消关注成功',U("Index/Question/show/id/$qid"),2);

        }

    }


}